const {  update, remove, add, getCount, query, getOne } = require('../../utils/dbTools');
const tableName = 'appointment';
const patientTableName = 'patient';
const staffTableName = 'staff';

/**
 * 获取数据总条数
 * 
*/

const appointmentTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const result = await getCount(tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}

/**
 * 获取预约列表
 * 
*/
const appointmentList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query;
    if (!page || !size) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // const map = { page, size };
    // const result = await getAll(tableName, map);
    let sql = `select a.id,a.patientid,a.appoint_time,a.status,a.create_time,p.name as patient, p.phone as patientPhone,s.name as doctor,s.phone as doctorPhone`
    sql += ` from ${tableName} a inner join ${patientTableName} p on a.patientid=p.id`;
    sql += ` inner join ${staffTableName} s on a.doctorid=s.id`;
    sql += ` order by a.id desc limit ${(page-1)*size},${size}`;

    const result = await query(sql);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}

/**
 * 新增预约信息
 * 
*/

const appointmentAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { patientid, appoint_time, status } = req.body;
    if (!patientid || !appoint_time || !status) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // 将日期字符串转为时间戳
    appoint_time = new Date(appoint_time).getTime();
    // 获取主治医生id
    const doctorid = await getDoctorInfo(patientid);
    const data = {
        patientid, doctorid, appoint_time, status, create_time: Date.now()
    }
    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
        return res.send(output);
    }
    res.send(output);
}

/**
 *  删除预约信息
 * 
*/

const appointmentRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }

    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }

    const result = await remove(id, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

/**
 * 更新预约信息: 仅支持更新状态
*/
const appointmentUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { id, patientid, appoint_time, status } = req.body;
    if (!id || !patientid || !appoint_time || !status) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // 将日期字符串转为时间戳
    appoint_time = new Date(appoint_time).getTime();
    // 获取主治医生id
    const doctorid = await getDoctorInfo(patientid);
    const result = await update(id, { patientid, doctorid, appoint_time, status }, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}



/**
 * id: 患者id
 * 
*/
const getDoctorInfo = async (id) => {
    const result = await getOne(staffTableName, { id });
    if (result) {
        return result.id;
    }
    return 0;
}


module.exports = {
    appointmentAdd,
    appointmentRemove,
    appointmentUpdate,
    appointmentList,
    appointmentTotal
}